﻿// -------------------------------------------------------------------------
//    @FileName         :    NFSysMsgUtil.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-10-28
//    @Email			:    445267987@qq.com
//    @Module           :    NFSysMsgUtil
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFSysMsgUtil.h"

#include "NFPacketSendUtil.h"
#include "NFPlayerUtil.h"
#include "NFPlayer.h"
#include "DescStore/BroadCastDesc.h"
#include "NFComm/NFPluginModule/NFRoute.h"
#include "NFServerComm/NFServerCommon/NFIServerMessageModule.h"

int NFSysMsgUtil::MakePlayerEscapePara(proto_ff::SysMsgEscapePara& para, uint64_t ullRoleID, const std::string& strName)
{
    para.set_role_id(ullRoleID);

    NFPlayer* pPlayer = NFPlayerUtil::GetPlayerByRoleID(ullRoleID);
    if (pPlayer != nullptr)
    {
        para.set_string_para(pPlayer->GetRoleName());
    }
    else if (!strName.empty())
    {
        para.set_string_para(strName);
    }
    else
    {
        para.set_string_para("玩家已下线!");
    }

    return 0;
}

int NFSysMsgUtil::MakeIntEscapePara(proto_ff::SysMsgEscapePara& para, int iValue)
{
    para.set_obj_id(iValue);
    return 0;
}

int NFSysMsgUtil::MakeULLEscapePara(proto_ff::SysMsgEscapePara& para, uint64_t ullValue)
{
    para.set_role_id(ullValue);
    return 0;
}

int NFSysMsgUtil::MakeIDEscapePara(proto_ff::SysMsgEscapePara& para, int iId)
{
    para.set_desc_id(iId);
    return 0;
}

int NFSysMsgUtil::MakeStringEscapePara(proto_ff::SysMsgEscapePara& para, const std::string& strPara)
{
    para.set_string_para(strPara);
    return 0;
}

int NFSysMsgUtil::MakeMessage(proto_ff::Proto_CSRsp* pRsp, int iId, const std::vector<proto_ff::SysMsgEscapePara>& vecEscapePara)
{
    CHECK_NULL(0, pRsp);

    auto pSysMsgDesc = BroadCastDesc::Instance()->GetDesc(iId);
    CHECK_NULL_WF(0, pSysMsgDesc, "can't find broad cast desc: %d", iId);

    proto_ff::Proto_CSSysMsgNtf& rNtf = *(pRsp->mutable_sysmsg_ntf());
    rNtf.set_desc_id(iId);
    for (int i = 0; i < (int)vecEscapePara.size(); i++)
    {
        *rNtf.add_escape_paras() = vecEscapePara[i];
    }

    return 0;
}

int NFSysMsgUtil::SendToPlayer(NFPlayer* pPlayer, int iId, const std::vector<proto_ff::SysMsgEscapePara>& vecEscapePara)
{
    int iRetCode = 0;

    CHECK_NULL(0, pPlayer);

    proto_ff::Proto_CSRsp stRsp;

    iRetCode = MakeMessage(&stRsp, iId, vecEscapePara);
    CHECK_RET(iRetCode, "make message error");

    return NFPacketSendUtil::SendToClient(pPlayer, proto_ff::E_CS_SYSMSG_NTF, &stRsp);
}

int NFSysMsgUtil::Broadcast(int iId)
{
    proto_ff::Proto_CSRsp rsp;
    proto_ff::Proto_CSSysMsgNtf& ntf = *(rsp.mutable_sysmsg_ntf());
    ntf.set_desc_id(iId);

    return NFPacketSendUtil::BroadcastAllLogic(proto_ff::E_CS_SYSMSG_NTF, &rsp);
}

int NFSysMsgUtil::Broadcast(int iId, const std::vector<proto_ff::SysMsgEscapePara>& vecEscapePara)
{
    proto_ff::Proto_CSRsp rsp;
    proto_ff::Proto_CSSysMsgNtf& ntf = *(rsp.mutable_sysmsg_ntf());
    ntf.set_desc_id(iId);
    for (int i = 0; i < (int)vecEscapePara.size(); i++)
    {
        *ntf.add_escape_paras() = vecEscapePara[i];
    }

    return NFPacketSendUtil::BroadcastAllLogic(proto_ff::E_CS_SYSMSG_NTF, &rsp);
}

int NFSysMsgUtil::Broadcast(proto_ff::SysMsgShowType eShowType, int iShowTime, const std::string& content)
{
    proto_ff::Proto_CSRsp rsp;
    proto_ff::Proto_CSSysMsgNtf& ntf = *(rsp.mutable_sysmsg_ntf());
    ntf.set_desc_id(0);
    ntf.set_content(content);
    ntf.set_show_type(eShowType);
    ntf.set_show_time(iShowTime);

    return NFPacketSendUtil::BroadcastAllLogic(proto_ff::E_CS_SYSMSG_NTF, &rsp);
}

int NFSysMsgUtil::SendSysMsg(NFPlayer* pPlayer, proto_ff::SysMsgShowType eShowType, int iShowTime, const std::string& content, const std::vector<proto_ff::SysMsgEscapePara>& vecEscapePara)
{
    proto_ff::Proto_CSRsp rsp;
    proto_ff::Proto_CSSysMsgNtf& ntf = *(rsp.mutable_sysmsg_ntf());
    ntf.set_desc_id(0);
    ntf.set_content(content);
    ntf.set_show_type(eShowType);
    ntf.set_show_time(iShowTime);
    for (int i = 0; i < (int)vecEscapePara.size(); i++)
    {
        *ntf.add_escape_paras() = vecEscapePara[i];
    }

    return NFPacketSendUtil::SendToClient(pPlayer, proto_ff::E_CS_SYSMSG_NTF, &rsp);
}
